In [1]:
from __future__ import absolute_import, division, print_function
In [2]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
In [3]:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("../mnist-data/", one_hot=True)
In [4]:
mnist.train.images.shape
Out[4]:
In [5]:
plt.figure(figsize=(15,5))
for i in list(range(10)):
plt.subplot(1, 10, i+1)
pixels = mnist.test.images[i]
pixels = pixels.reshape((28, 28))
plt.imshow(pixels, cmap='gray_r')
plt.show()
In [6]:
from keras.models import Model
from keras.layers import Input, Dense, Activation
from keras.layers import Dropout, Flatten, Reshape, merge
from keras.layers import Convolution2D, MaxPooling2D, AveragePooling2D
from keras.layers import BatchNormalization
In [7]:
from functools import reduce
def print_layers(model):
for l in model.layers:
print(l.name, l.output_shape, [reduce(lambda x, y: x*y, w.shape) for w in l.get_weights()])
In [8]:
def BNConv(nb_filter, nb_row, nb_col, subsample=(1, 1), border_mode="same"):
def f(input):
conv = Convolution2D(nb_filter=nb_filter, nb_row=nb_row, nb_col=nb_col, subsample=subsample,
border_mode=border_mode, activation="relu", init="he_normal")(input)
return BatchNormalization()(conv)
return f
In [9]:
def inception_naive_module(m=1):
def f(input):
# Tower A
conv_a = BNConv(32*m, 1, 1)(input)
# Tower B
conv_b = BNConv(32*m, 3, 3)(input)
# Tower C
conv_c = BNConv(16*m, 5, 5)(input)
# Tower D
pool_d = MaxPooling2D(pool_size=(3, 3), strides=(1, 1), border_mode="same")(input)
return merge([conv_a, conv_b, conv_c, pool_d], mode='concat', concat_axis=3)
return f
In [10]:
def inception_dimred_module(m=1):
def f(input):
# Tower A
conv_a = BNConv(32*m, 1, 1)(input)
# Tower B
conv_b = BNConv(16*m, 1, 1)(input)
conv_b = BNConv(32*m, 3, 3)(conv_b)
# Tower C
conv_c = BNConv(4*m, 1, 1)(input)
conv_c = BNConv(16*m, 5, 5)(conv_c)
# Tower D
# max pooling followed by compression
pool_d = MaxPooling2D(pool_size=(3, 3), strides=(1, 1), border_mode="same")(input)
conv_d = BNConv(16*m, 1, 1)(pool_d)
return merge([conv_a, conv_b, conv_c, conv_d], mode='concat', concat_axis=3)
return f
In [11]:
def inception(version='v2'):
#select inception module
if version=='v2' :
inception_module = inception_dimred_module
else:
# naive version, big filter banks,
# stacking = too many params!
inception_module = inception_naive_module
#input in the right shape, tensorflow ordered
_in = Input(shape=(784,))
reshape_1 = Reshape((28,28,1))(_in)
# go to 32 channels
conv_0 = BNConv(32, 3, 3)(reshape_1)
conv_0 = BNConv(32, 3, 3)(conv_0)
pool_0 = MaxPooling2D((2, 2))(conv_0)
# apply inception network (input: 14x14x32, output channels:96)
module_1 = inception_module()(pool_0)
# pool to 7x7x96
pool_1 = MaxPooling2D((2, 2))(module_1)
# apply inception network (input: 7x7x96, output channels:192)
module_2 = inception_module(m=2)(pool_1)
# pool to: 1x1x96 and flatten
x = AveragePooling2D((7, 7))(module_2)
x = Flatten()(x)
x = Dropout(0.4)(x)
# dense layer and normalization
fc = Dense(128, activation='relu')(x)
fc = BatchNormalization()(fc)
_out = Dense(10, activation='softmax')(fc)
model = Model(_in, _out)
return model
In [12]:
# loosely adapted from https://arxiv.org/pdf/1409.4842v1.pdf
model = inception()
print_layers(model)
In [13]:
# https://arxiv.org/pdf/1409.4842v1.pdf
from keras.optimizers import Adam
model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=0.01), metrics=["accuracy"])
model.fit(mnist.train.images, mnist.train.labels,
batch_size=128, nb_epoch=10, verbose=1,
validation_data=(mnist.test.images, mnist.test.labels))
Out[13]:
In [14]:
model.fit(mnist.train.images, mnist.train.labels,
batch_size=128, nb_epoch=10, verbose=1,
validation_data=(mnist.test.images, mnist.test.labels))
Out[14]:
In [ ]:
model.fit(mnist.train.images, mnist.train.labels,
batch_size=128, nb_epoch=10, verbose=1,
validation_data=(mnist.test.images, mnist.test.labels))
In [ ]: